<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button v-if="hasPermission('post:save')" type="primary" @click="handleCreate">
          新增岗位
        </a-button>
        <a-button
          v-if="hasPermission('post:delete')"
          type="primary"
          :disabled="wacthEdit()"
          @click="handleDelete"
          danger
        >
          批量删除
        </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '编辑',
                auth: 'post:update',
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '删除',
                icon: 'ant-design:delete-outlined',
                auth: 'post:delete',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <SysPostModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { useModal } from '@/components/Modal';
  import SysPostModal from './sys-post-modal.vue';
  import { listPostApi, delPostApi } from '@/api/system/post';
  import { columns, searchFormSchema } from './data';
  import { message } from 'ant-design-vue';
  import { usePermission } from '@/hooks/web/usePermission';
  import { useMessage } from '@/hooks/web/useMessage';
  import { h } from 'vue';

  const { hasPermission } = usePermission();
  const { createConfirm } = useMessage();
  const [registerTable, { reload, getSelectRows }] = useTable({
    title: '岗位分类',
    rowKey: 'id',
    api: listPostApi,
    columns,
    formConfig: {
      schemas: searchFormSchema,
      labelWidth: 70,
    },
    rowSelection: {
      type: 'checkbox',
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    actionColumn: {
      width: 160,
      title: '操作',
      dataIndex: 'action',
      fixed: undefined,
    },
  });

  const [registerModal, { openModal }] = useModal();

  function wacthEdit() {
    const res = getSelectRows();
    if (res.length > 0) {
      return false;
    } else {
      return true;
    }
  }

  function handleCreate() {
    openModal(true, {});
  }

  function handleEdit(record: Recordable) {
    console.log(record);
    openModal(true, record);
  }

  async function handleDelete(record: Recordable) {
    if (record.id) {
      await delPostApi(record.id);
      reload();
    } else {
      const delData = getSelectRows();
      if (delData.length === 0) {
        message.warning('请选择数据');
        return;
      }
      createConfirm({
        iconType: 'warning',
        title: () => h('span', '提示'),
        content: () => h('span', `确认删除${delData.map((item) => item.postName).join('、')}吗？`),
        onOk: async () => {
          await delPostApi(delData.map((item) => item.id).join(','));
          reload();
        },
      });
    }
  }

  function handleSuccess() {
    reload();
  }
</script>
